<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="集群名称" prop="clusterName">
        <el-input
            v-model="queryParams.clusterName"
            placeholder="请输入集群名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['resource:cluster:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList(false)"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="clusterList" @selection-change="handleSelectionChange"  border  highlight-current-row align="center">
      <el-table-column label="主键ID" align="center" prop="id"/>
      <el-table-column label="集群名称" align="center" prop="clusterName"/>
      <el-table-column label="Flink版本" align="center" prop="flinkVersion"/>
      <el-table-column label="yarn替换地址" align="center" prop="mapUrl"/>
      <el-table-column label="hadoop配置" align="center" prop="hadoopDir"/>
      <el-table-column label="是否开启kerberos" align="center" prop="isKereros">
        <template slot-scope="scope">
          <TableState v-if="boolFunc(scope.row.isKereros)" showTitle="已开启" stateType="positive"/>
          <TableState v-else showTitle="未开启" stateType=""/>
        </template>
      </el-table-column>
      <el-table-column label="kereros配置" align="center" prop="kererosDir"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="160"/>
      <el-table-column prop="isEnable" label="是否启用" align="center" min-width="200">
        <template slot-scope="scope">
          <TableState v-if="boolFunc(scope.row.isEnable)" showTitle="已启用" stateType="positive"/>
          <TableState v-else showTitle="未启用" stateType=""/>
        </template>
      </el-table-column>
      <el-table-column prop="isInUse" label="使用情况" align="center" min-width="200">
        <template slot-scope="scope">
          <TableState v-if="boolFunc(scope.row.isInUse)" showTitle="集群正在使用" stateType="positive"/>
          <TableState v-else showTitle="集群未使用" stateType=""/>
        </template>
      </el-table-column>

      <el-table-column prop="createSuccess" label="是否创建完成" align="center" min-width="200">
        <template slot-scope="scope">
          <TableState v-if="scope.row.createSuccess" showTitle="创建完成" stateType="positive"/>
          <TableState v-else showTitle="创建中" stateType=""/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="!boolFunc(scope.row.isInUse)"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
                     v-hasPermi="['resource:cluster:edit']"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['resource:cluster:remove']"
              v-if="!boolFunc(scope.row.isInUse)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList(false)"
    />

    <!-- 添加或修改集群管理表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" :close-on-click-modal="false" append-to-body
               :before-close="closeDialog">
      <addYarnCluster :formData="formData" ref="yarnClusterInfoRef"
                      @handelClusterSaveOk="handelClusterSaveOk"/>
    </el-dialog>
  </div>
</template>

<script>
import {delCluster, getCluster, listCluster} from "@/api/resource/cluster";
import TableState from "@/components/TableState";
import addYarnCluster from "@/views/resource/cluster/addYarnCluster";
import {str2Bool} from "@/utils/ruoyi";
import {getAllFlinkversions} from "@/api/resource/flinkversions";

export default {
  name: "Cluster",
  components: {
    TableState,
    addYarnCluster
  },
  data() {
    return {
      currentFlinkVersions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 集群管理表表格数据
      clusterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clusterName: null,
        clusterType: null,
        isEnable: null,
        hadoopDir: null,
        isKereros: null,
        kererosDir: null,
        isInUse: null
      },
      formData: {
        id: '',
        clusterName: '',
        hadoopDir: '',
        isKereros: false,
        kererosDir: '',
        isEnable: true,
        flinkVersion: '',
        mapUrl:'',
        flinkVersionList: []
      },
    };
  },
  mounted() {
    const timer = window.setInterval(() => this.getList(true), 2000)
    this.$once('hook:beforeDestroy', () => {
      clearInterval(timer)
    })
  },
  created() {
    this.getList(false);
  },
  methods: {
    boolFunc(v) {
      console.log(v)
      return str2Bool(v);
    },

    /** 查询集群管理表列表 */
    getList(isTimer) {
      if(!isTimer)
      {
        this.loading = true;
      }
      listCluster(this.queryParams).then(response => {
        this.clusterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },

    closeDialog() {
      if (this.$refs.yarnClusterInfoRef !== undefined) {
        this.$refs.yarnClusterInfoRef.resetForm()
      }
      this.open = false;
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList(false);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    saveInfo(editType, row) {
      getAllFlinkversions().then(res => {
        if (res.rows.length <= 0) {
          this.$message.warning('请先设置flink环境！')
          return
        }
        this.formData.flinkVersionList = res.rows.map(item => {
          return {
            flinkVersion: item.flinkVersion
          }
        });
        if (editType === 'add') {
          this.open = true;
          this.title = "添加集群管理表";
        } else {
          const id = row.id || this.ids
          getCluster(id).then(response => {
            if (str2Bool(response.data.isInUse)) {
              this.$message.warning('正在使用的集群不能修改！')
              return
            }
            this.formData.id = response.data.id;
            this.formData.clusterName = response.data.clusterName;
            this.formData.hadoopDir = response.data.hadoopDir;
            this.formData.isKereros = str2Bool(response.data.isKereros);
            this.formData.kererosDir = response.data.kererosDir;
            this.formData.isEnable = str2Bool(response.data.isEnable);
            this.formData.flinkVersion = response.data.flinkVersion;
            this.open = true;
            this.title = "修改集群管理表";
          });
        }
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.saveInfo('add', null);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.saveInfo('edite', row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除集群管理表编号为"' + ids + '"的数据项？').then(function () {
        return delCluster(ids);
      }).then(() => {
        this.getList(false);
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('resource/cluster/export', {
        ...this.queryParams
      }, `cluster_${new Date().getTime()}.xlsx`)
    },

    /**
     * 保存回调
     * @param v
     */
    handelClusterSaveOk(v) {
      this.open = v;
      this.getList(false);
      this.clearForm();
    },


    clearForm() {
      this.formData = {
        id: '',
        clusterName: '',
        hadoopDir: '',
        isKereros: false,
        kererosDir: '',
        isEnable: true,
        flinkVersion: '',
        mapUrl:'',
        flinkVersionList: []
      }
    }

  }
};
</script>
